Page({
  data: {
    birthDate: '2004-01',
    gender: '男职工',
    personTypes: ['男职工', '原法定退休年龄55周岁女职工', '原法定退休年龄50周岁女职工'],
    selectedIndex: 0,
    retirementAge: null,
    retirementDate: null,
    delayMonths: null,
    showDatePicker: false,
    showTypePicker: false,
    currentDate: new Date('2004-01').getTime(),
    minDate: new Date('1900-01').getTime(),
    maxDate: new Date('2100-12').getTime(),
  },

  showDatePicker() {
    this.setData({ showDatePicker: true });
  },

  onCloseDatePicker() {
    this.setData({ showDatePicker: false });
  },

  onConfirmDate(event) {
    const date = new Date(event.detail);
    const birthDate = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}`;
    this.setData({
      birthDate,
      showDatePicker: false
    });
  },

  showTypePicker() {
    this.setData({ showTypePicker: true });
  },

  onCloseTypePicker() {
    this.setData({ showTypePicker: false });
  },

  onConfirmType(event) {
    const { index, value } = event.detail;
    this.setData({
      gender: value,
      selectedIndex: index,
      showTypePicker: false
    });
  },

  calculateRetirement() {
    let res = {}
    const birthday = this.data.birthDate
    switch (this.data.gender) {
      case '男职工':
        res = this.calc_new_retire('1965-01', 60, 63, 4, birthday)
        break;
      case '原法定退休年龄55周岁女职工':
        res = this.calc_new_retire('1970-01', 55, 58, 4, birthday)
        break;
      case '原法定退休年龄50周岁女职工':
        res = this.calc_new_retire('1975-01', 50, 55, 2, birthday)
        break;
    }
    console.log(res);

    this.setData({
      retirementAge: res.new_retire_year,
      retirementDate: res.new_retire_date,
      delayMonths: res.total_delay_month
    });
  },

  reset() {
    this.setData({
      birthDate: '2004-01',
      gender: '男职工',
      selectedIndex: 0,
      retirementAge: null,
      retirementDate: null,
      delayMonths: null
    });
  },

  calc_new_retire: function (begin_date, old_retire_year, new_retire_year, step, input_date) {
    //1.日期格式验证
    // 日期正则表达式：匹配 YYYY-M, YYYY-MM, YYYY-M-D 或 YYYY-MM-DD 格式
    var valid_date = /^\d{4}-\d{1,2}(-\d{1,2})?$/;
    if (!valid_date.test(begin_date)) {
      console.error('invalid date : ' + begin_date)
      return false;
    }
    if (!valid_date.test(input_date)) {
      console.error('invalid date : ' + input_date)
      return false;
    }

    var [begin_date_year, begin_date_month] = begin_date.split('-').map(v => parseInt(v));
    var [input_date_year, input_date_month] = input_date.split('-').map(v => parseInt(v));

    //2. 在初始日期之前，按原定的退休年龄进行退休
    if ((input_date_year < begin_date_year) || (input_date_year === begin_date_year && input_date_month < begin_date_month)) {
      return {
        input_date: input_date,
        new_retire_year: old_retire_year,
        new_retire_month: 0,
        new_retire_date: (input_date_year + old_retire_year) + '-' + input_date_month,
        total_delay_month: 0
      }
    }

    var delay_year = new_retire_year - old_retire_year;
    var cost_year = delay_year * step;
    var end_date_year = begin_date_year + cost_year;
    var end_date_month = begin_date_month;

    //3. 在结束日期之后，按新的退休年龄进行退休
    if (input_date_year > end_date_year || (input_date_year === end_date_year && input_date_month >= end_date_month)) {
      return {
        input_date: input_date,
        new_retire_year: new_retire_year,
        new_retire_month: 0,
        new_retire_date: (input_date_year + new_retire_year) + '-' + input_date_month,
        total_delay_month: delay_year * 12
      }
    }

    //4. 在开始和结束日期之中，则计算延迟退休时间
    var months_diff = (input_date_year - begin_date_year) * 12 + (input_date_month - (begin_date_month - 1))
    var total_delay_month = Math.ceil(months_diff / step); // 延迟总月份数

    var add_month = input_date_month + (total_delay_month % 12);
    var new_date_month, new_date_year;
    if (add_month > 12) {
      new_date_month = add_month % 12;
      new_date_year = input_date_year + Math.floor(total_delay_month / 12) + 1;
    } else {
      new_date_month = add_month;
      new_date_year = input_date_year + Math.floor(total_delay_month / 12)
    }

    return {
      input_date: input_date,
      new_retire_year: old_retire_year + Math.floor(total_delay_month / 12),
      new_retire_month: total_delay_month % 12,
      new_retire_date: (new_date_year + old_retire_year) + '-' + new_date_month,
      total_delay_month: total_delay_month
    }
  }
})